package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.service.admin.MarketCommentService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketCommentServiceImpl implements MarketCommentService {

    @Autowired
    MarketCommentMapper commentMapper;
    @Override
    public void delete(Integer id) {

        MarketComment comment = new MarketComment();
        comment.setId(id);
        comment.setDeleted(true);
        comment.setUpdateTime(LocalDateTime.now());
        //逻辑删除
        commentMapper.updateByPrimaryKeySelective(comment);
        //物理删除
        //commentMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<MarketComment> list(Integer page, Integer limit, String userId, String valueId, String sort, String order) {

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.or();
        //判空
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        criteria.andDeletedEqualTo(false);
        marketCommentExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketComment> marketComments = commentMapper.selectByExample(marketCommentExample);

        return marketComments;
    }
}
